package org.jeecg.modules.parameter.biz.controlltr;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.parameter.api.dto.station.SimSaveStationDto;
import org.jeecg.modules.parameter.api.entity.SimStation;
import org.jeecg.modules.parameter.api.vo.exit.SimExitVo;
import org.jeecg.modules.parameter.api.vo.hall.SimHallVo;
import org.jeecg.modules.parameter.api.vo.line.SimLineColorVo;
import org.jeecg.modules.parameter.api.vo.platform.SimPlatformVo;
import org.jeecg.modules.parameter.api.vo.station.*;
import org.jeecg.modules.parameter.biz.service.*;
import org.jeecg.modules.parameter.common.api.dto.PatchPO;
import org.jeecg.modules.parameter.common.utils.oConvertUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author P
 */
@Api(tags = "参数管理-车站管理")
@RestController
@RequestMapping(value = {"/stationInfo"}, produces = {"application/json; charset=utf-8"})
@Slf4j
public class SimStationController extends JeecgController<SimStation, SimStationService> {

    //线路service
    @Resource
    private SimLineService lineService;
    //车站service
    @Resource
    private SimStationService stationService;

    //站台service
    @Resource
    private SimStationPlatformService platformService;
    //站厅service
    @Resource
    private SimStationHallService simStationHallService;

    //出入口service
    @Resource
    private SimStationExitService simStationExitService;

    @ApiOperation(value = "车站管理-查询所有基于线路的车站", notes = "车站管理-查询所有基于线路的车站")
    @PostMapping(value = "/selectStation")
    public Result<List<SimLineAndStationVo>> selectStation() {
        List<SimLineAndStationVo> simLineAndStationVo = oConvertUtils.entityListToModelList(lineService.getAllLine(), SimLineAndStationVo.class);
        List<StationVo> stationVo = oConvertUtils.entityListToModelList(stationService.getAllStation(), StationVo.class);
        for (SimLineAndStationVo lineAndStation : simLineAndStationVo) {
            List<StationVo> filteredList = stationVo.stream()
                    .filter(item -> lineAndStation.getLineId().equals(item.getLineId()))
                    .collect(Collectors.toList());
            lineAndStation.setStations(filteredList);
        }
        return Result.ok(simLineAndStationVo);
    }


    @ApiOperation(value = "车站管理-按线路ID查询车站信息", notes = "车站管理-按线路ID查询车站信息")
    @PostMapping(value = "/queryStationByLineId")
    public Result<SimStationByLineIdVo> queryStationByLineId(@RequestParam(name = "lineId") String id) {

        SimStationByLineIdVo lineVo = null;
        List<SimStationByLineIdVo> lines = oConvertUtils.entityListToModelList(lineService.getAllLine(), SimStationByLineIdVo.class);
        List<StationByLineIdVo> stations = oConvertUtils.entityListToModelList(stationService.getStationByLineId(id), StationByLineIdVo.class);
        List<SimLineColorVo> color = oConvertUtils.entityListToModelList(lines, SimLineColorVo.class);


        for (StationByLineIdVo station : stations) {
            if ("1".equals(station.getTransferFlag())) {
                String transferLineId = station.getTransferLineId();
                String[] transferLineIds = transferLineId.split(",");
                Map<String, Object> lineColorTemp = new HashMap<>();

                for (String lineId : transferLineIds) {

                    for (SimLineColorVo col : color) {

                        if (!Objects.equals(lineId, station.getLineId()) && col.getLineId().equals(lineId)) {
                            lineColorTemp.put("lineId", lineId);
                            lineColorTemp.put("color", col.getColor());
                            break;
                        }
                    }
                }
                station.setTransferLineColor(lineColorTemp);

            }
            lineVo = lines.stream()
                    .filter(line -> line.getLineId().equals(station.getLineId()))
                    .collect(Collectors.toList()).get(0);
            lineVo.setStations(stations);
        }
        return Result.ok(lineVo);
    }





    @ApiOperation(value = "车站管理-按ID查询车站及下及信息", notes = "车站管理-按ID查询车站及下及信息")
    @PostMapping(value = "/queryAllByStatId")
    public Result<StationAllVo> queryByStatId(@RequestParam(name = "stationUUID") String id) {

        //查询车站信息基于ID
        StationAllVo station = oConvertUtils.entityToModel(stationService.queryById(id), StationAllVo.class);
        //查询站厅集合
        List<SimHallVo> simHallVo = oConvertUtils.entityListToModelList(simStationHallService.getHallBySataId(id), SimHallVo.class);
        //查询站台集合
        List<SimPlatformVo> simPlatformVo = oConvertUtils.entityListToModelList(platformService.getPlatformBySataId(id), SimPlatformVo.class);
        //查询出入口集合
        List<SimExitVo> simExitVo = oConvertUtils.entityListToModelList(simStationExitService.getExitBySataId(id), SimExitVo.class);
        //插入到车站信息内
        assert station != null;
        station.setSimHall(simHallVo);
        station.setSimPlatform(simPlatformVo);
        station.setSimExit(simExitVo);
        return Result.ok(station);
    }


    @ApiOperation(value = "车站管理-按线路ID查询车站-选择器", notes = "车站管理-按线路ID查询车站-选择器")
    @PostMapping(value = "/queryStationOptions")
    public Result<List<StationOptionVo>> queryStationOptions(@RequestParam(name = "lineId") String id) {

        return Result.ok(stationService.getStationOptions(id));
    }

    @ApiOperation(value = "车站管理-添加车站", notes = "车站管理-添加车站")
    @PostMapping(value = "/addStation")
    public Result<Boolean> saveStation(@RequestBody  SimSaveStationDto saveStationDto) {
        SimStation stations = oConvertUtils.entityToModel(saveStationDto, SimStation.class);
        return Result.ok(stationService.save(stations));
    }


    @ApiOperation(value = "车站管理-删除车站", notes = "车站管理-删除车站")
    @DeleteMapping(value = "/deleteStation")
    public Result<Boolean> delStation(@RequestParam(name = "id") String id) {
        return Result.ok(stationService.removeById(id));
    }
    @ApiOperation(value = "车站管理-修改车站", notes = "车站管理-修改车站")
    @PutMapping(value = "/updateStation")
    public Result<String> updateStation(@RequestBody PatchPO patchPO) {
        stationService.updatePatch(patchPO);
        return Result.OK("更新成功!");
    }
    @ApiOperation(value = "车站管理-按ID查询车站", notes = "车站管理-按ID查询车站信息")
    @PostMapping(value = "/queryById")
    public Result<SimQueryByIdStationVo> queryById(@RequestParam(name = "id") String id) {
        SimQueryByIdStationVo station = oConvertUtils.entityToModel(stationService.queryById(id), SimQueryByIdStationVo.class);
        return Result.ok(station);
    }

}
